home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / utils / nroff~06.zoo / main.c < prev    next >
C/C++ Source or Header  |  1992-07-16  |  25KB  |  1,142 lines

  1. static char *rcsid_main_c="$Id: main.c,v 1.2 1992/07/16 10:38:32 rosenkra Exp $";
  2.  
  3. /*
  4.  * $Log: main.c,v $
  5.  * Revision 1.2  1992/07/16  10:38:32  rosenkra
  6.  * port to gcc, add tm,ie,el
  7.  *
  8.  */
  9.  
  10. #include "config.h"
  11.  
  12. /*
  13.  *    main.c - main for nroff word processor
  14.  *
  15.  *    similar to Unix(tm) nroff or RSX-11M RNO. adaptation of text processor
  16.  *    given in "Software Tools", Kernighan and Plauger.
  17.  *
  18.  *    adapted for atariST/TOS by Bill Rosenkranz 11/89
  19.  *    net:    rosenkra@convex.com
  20.  *    CIS:    71460,17
  21.  *    GENIE:    W.ROSENKRANZ
  22.  *
  23.  *    original author:
  24.  *
  25.  *    Stephen L. Browning
  26.  *    5723 North Parker Avenue
  27.  *    Indianapolis, Indiana 46220
  28.  *
  29.  *    history:
  30.  *
  31.  *    - Originally written in BDS C;
  32.  *    - Adapted for standard C by W. N. Paul
  33.  *    - Heavily hacked up to conform to "real" nroff by Bill Rosenkranz
  34.  */
  35.  
  36. #define NRO_MAIN            /* to define globals in nro.h */
  37.  
  38. #include <stdio.h>
  39. #ifdef GEMDOS
  40. # ifdef __GNUC__
  41. #  include <types.h>
  42. #  include <time.h>
  43. #  include <stdlib.h>
  44. #  include <string.h>
  45. #  include <unistd.h>
  46. # else
  47. #  include <sys\types.h>
  48. #  include <sys\time.h>
  49. # endif
  50. #else
  51. # include <sys/types.h>
  52. # include <sys/time.h>
  53. #endif
  54. #include "nroff.h"
  55.  
  56.  
  57. /*------------------------------*/
  58. /*    main            */
  59. /*------------------------------*/
  60. void main (argc, argv)
  61. int     argc;
  62. char   *argv[];
  63. {
  64.     REGISTER int    i;
  65.     int        swflg;
  66.     int        ifp = 0;
  67.     char           *ptmp;
  68. #ifndef GEMDOS
  69.     char           *pterm;
  70.     char        capability[100];
  71.     char           *pcap;
  72.     char           *ps;
  73. #endif
  74.  
  75.  
  76.  
  77.     /*
  78.      *   set up initial flags and file descriptors
  79.      */
  80.     swflg       = FALSE;
  81.     ignoring    = FALSE;
  82.     hold_screen = FALSE;
  83.     debugging   = FALSE;
  84.     stepping    = FALSE;
  85.     mc_ing      = FALSE;
  86.     out_stream  = stdout;
  87.     err_stream  = stderr;
  88.     dbg_stream  = stderr;
  89.  
  90.  
  91.     /*
  92.      *   this is for tmp files, if ever needed. it SHOULD start
  93.      *   out without the trailing slash. if not in env, use default
  94.      */
  95.     if (ptmp = getenv ("TMPDIR"))
  96.         strcpy (tmpdir, ptmp);
  97.     else
  98.         strcpy (tmpdir, ".");
  99.  
  100.  
  101.     /*
  102.      *   initialize structures (defaults)
  103.      */
  104.     init ();
  105.     ex_macro[0] = '\0';
  106.     ex_macro[1] = '\0';
  107.     ex_macro[2] = '\0';
  108.     ex_macro[3] = '\0';
  109.     ex_macro[4] = '\0';
  110.  
  111.  
  112.     /*
  113.      *   parse cmdline flags
  114.      */
  115.     for (i = 1; i < argc; ++i)
  116.     {
  117.         if (*argv[i] == '-' || *argv[i] == '+')
  118.         {
  119.             if (pswitch (argv[i], &swflg) == ERR)
  120.                 err_exit (-1);
  121.         }
  122.     }
  123.  
  124.  
  125.     /*
  126.      *   handle terminal for \fB, \fI
  127.      */
  128. #ifdef GEMDOS
  129.     if (ansi_escapes)
  130.     {
  131.         /*
  132.          *   this is new a p4. i checked a lot of termcap entries,
  133.          *   and found that "ESC [ n m" is common to turn on certain
  134.          *   capabilities (like standout and underline, but that
  135.          *   "ESC [ m" was more common than "ESC [ 0 m" to turn them
  136.          *   off. i perfer the "0" but that does not appear to work
  137.          *   with vt100's.
  138.          *
  139.          *   vt100 supports standout (so) which i map to standout,
  140.          *   bold (md) which i map to bold, rev video (standout), and
  141.          *   us (underline) which i map to italic. the others will
  142.          *   do nothing. there is currently no way to get rev video
  143.          *   and small character escape sequences out of nroff.
  144.          *   it only supports R, B, I, and S. both ms and man have
  145.          *   a ".SM t" macro for small text, perhaps this could be
  146.          *   added to nroff itself. .SM macro changes point size,
  147.          *   which is normally ignored by nroff anyway. it is for
  148.          *   troff.
  149.          *
  150.          *   these codes work with manpager...
  151.          */
  152.         strcpy (s_bold,     "\33[1m");    /* \fB bold font */
  153.         strcpy (e_bold,     "\33[m");
  154.         strcpy (s_italic,   "\33[4m");    /* \fI italic/underline font */
  155.         strcpy (e_italic,   "\33[m");
  156.         strcpy (s_special,  "\33[9m");    /* \fS special/math font */
  157.         strcpy (e_special,  "\33[m");
  158.         strcpy (s_standout, "\33[7m");    /* standout/rev video font */
  159.         strcpy (e_standout, "\33[m");
  160.         strcpy (s_small,    "\33[8m");    /* small font */
  161.         strcpy (e_small,    "\33[m");
  162.         strcpy (e_all,      "\33[m");    /* \fR normal font */
  163.     }
  164.     else
  165.     {
  166.         strcpy (s_standout, "\33p");    /* atari/TOS is easy... */
  167.         strcpy (e_standout, "\33q");
  168.         strcpy (s_bold,     "\33p");    /* atari/TOS is easy... */
  169.         strcpy (e_bold,     "\33q");
  170.         strcpy (s_italic,   "\33p");
  171.         strcpy (e_italic,   "\33q");
  172.     }
  173. #else
  174.     s_standout[0] = '\0';
  175.     e_standout[0] = '\0';
  176.     s_bold[0]     = '\0';
  177.     e_bold[0]     = '\0';
  178.     s_italic[0]   = '\0';
  179.     e_italic[0]   = '\0';
  180.     if ((pterm = getenv ("TERM"))        /* it must exist first... */
  181.     && (tgetent (termcap, pterm) == 1))    /* ...so we fill buffer */
  182.     {
  183.         /*
  184.          *   we currently use standout mode for all weirdness
  185.          *   lile BOLD, italic, etc.
  186.          */
  187.         pcap = capability;
  188.         if (ps = tgetstr ("so", &pcap))
  189.         {
  190.             /*
  191.              *   sun has padding in here. this is NOT portable.
  192.              *   better to use tputs() to strip it...
  193.              */
  194.             while (*ps && *ps != 0x1b)    ps++;
  195.             strcpy (s_standout, ps);
  196.             strcpy (s_bold, ps);
  197.             strcpy (s_italic, ps);
  198.         }
  199.         if (ps = tgetstr ("se", &pcap))
  200.         {
  201.             while (*ps && *ps != 0x1b)    ps++;
  202.             strcpy (e_standout, ps);
  203.             strcpy (e_bold, ps);
  204.             strcpy (e_italic, ps);
  205.         }
  206.     }
  207. #endif
  208.  
  209.  
  210.  
  211.     /*
  212.      *   loop on files
  213.      */
  214.     for (i = 1; i < argc; ++i)
  215.     {
  216.         if (*argv[i] != '-' && *argv[i] != '+')
  217.         {
  218.             /*
  219.              *   open this file...
  220.              */
  221.             if ((sofile[0] = fopen (argv[i], "r")) == NULL_FPTR)
  222.             {
  223.                 fprintf (err_stream,
  224.                     "***%s: unable to open file %s\n",
  225.                     myname, argv[i]);
  226.                 err_exit (-1);
  227.             }
  228.             else
  229.             {
  230.                 /*
  231.                  *   do it for this file...
  232.                  */
  233.                 ifp = 1;
  234.                 profile ();
  235.                 fclose (sofile[0]);
  236.             }
  237.         }
  238.         else if (*argv[i] == '-' && *(argv[i]+1) == 0)
  239.         {
  240.             /*
  241.              *   - means read stdin (anywhere in file list)
  242.              */
  243.             sofile[0] = stdin;
  244.             ifp = 1;
  245.             profile ();
  246.         }
  247.  
  248.     }
  249.  
  250.     /*
  251.      *   if no files, usage (should really use stdin...)
  252.      */
  253. #ifdef USE_STDIN
  254.     if (ifp == 0)
  255.     {
  256.         sofile[0] = stdin;
  257.         ifp = 1;
  258.         profile ();
  259.     }
  260. #else
  261.     if ((ifp == 0 && swflg == FALSE) || argc <= 1)
  262.     {
  263.         usage ();
  264.  
  265.         err_exit (-1);
  266.     }
  267. #endif
  268.  
  269.  
  270.     /*
  271.      *   execute any exit macro (defined with .em)
  272.      */
  273.     if (ex_macro[1])
  274.     {
  275.         char    ibuf[MAXLINE];
  276.  
  277.         /*
  278.          *   comand will push any macro on the pb buffer so getlin
  279.          *   can do its thing
  280.          */
  281.         ex_macro[0] = dc.cmdchr;
  282.         comand (ex_macro);
  283.  
  284.         while (getlin (ibuf, NULL_FPTR) != EOF)
  285.         {
  286.             /*
  287.              *   if line is a command or text
  288.              */
  289.             if (ibuf[0] == dc.cmdchr)
  290.             {
  291.                 comand (ibuf);
  292.             }
  293.             else
  294.             {
  295.                 /*
  296.                  *   this is a text line. first see if
  297.                  *   first char is space. if it is, break
  298.                  *   line.
  299.                  */
  300.                 if (ibuf[0] == ' ')
  301.                     robrk ();
  302.                 text (ibuf);
  303.             }
  304.         }
  305.     }
  306.  
  307.  
  308.     /*
  309.      *   now finish last page (if ex_macro does .ex, we never get here)
  310.      */
  311.     if (pg.lineno > 0)
  312.         space (HUGE);
  313.  
  314.  
  315.     /*
  316.      *   normal exit. this will fflush/fclose streams...
  317.      */
  318.     err_exit (0);
  319.  
  320.     /*NOTREACHED*/
  321. }
  322.  
  323.  
  324.  
  325.  
  326. /*------------------------------*/
  327. /*    usage            */
  328. /*------------------------------*/
  329. void usage ()
  330. {
  331.     /*
  332.      *   note: -l may not work correctly
  333.      */
  334.     fprintf (stderr, "Usage:   %s [options] file [...]\n", myname);
  335.     fprintf (stderr, "Options: -a        no font changes\n");
  336.     fprintf (stderr, "         -T<name>  terminal (device) name\n");
  337.     fprintf (stderr, "                   (currently set for ANSI/vt100 escapes)\n");
  338.     fprintf (stderr, "         -b        backspace\n");
  339.     fprintf (stderr, "         -d        debug mode (stderr or err log if -e first)\n");
  340.     fprintf (stderr, "         -debug    debug mode (nroff.dbg)\n");
  341. #ifdef GEMDOS
  342.     fprintf (stderr, "         -h        hold screen before desktop\n");
  343. #endif
  344. /*!!!    fprintf (stderr, "         -l        output to printer\n");*/
  345.     fprintf (stderr, "         -m<name>  macro file (e.g. -man)\n");
  346.     fprintf (stderr, "         -e<file>  error log file (stderr is default)\n");
  347.     fprintf (stderr, "         -po<n>    page offset\n");
  348.     fprintf (stderr, "         -pn<n>    initial page number\n");
  349.     fprintf (stderr, "         -pl<n>    page length\n");
  350.     fprintf (stderr, "         -s        step through pages\n");
  351.     fprintf (stderr, "         -v        print version only\n");
  352.     fprintf (stderr, "         +<